<?php
require_once('../includes/config.inc');
require_once('../includes/variables.inc');

$rights = $_SESSION['rights'];

//session_register($rights);
//session_start();
//if ($_SESSION['rights']) $rights = $_SESSION['rights'];
//$_SESSION['rights'] = $rights;
//session_register($U_);
//$U_[rights] = $_SESSION['rights'];
?>
<html>
<head>
<!--//begin ajax crap code 1 of 2 -->
<script type="text/javascript" language="Javascript">
function createXMLHttpRequest() {

var ua; //this variable is for the user agent string for the browser

if(window.XMLHttpRequest) {
    try {
      ua = new XMLHttpRequest();
    } catch(e) {
      ua = false;
    }
  } else if(window.ActiveXObject) {
    try {
      ua = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      ua = false;
    }
  }
  return ua;
}
</script>
<!--//end ajax crap code 1 of 2 -->

<!--//begin ajax crap code 2 of 2 -->
<script type="text/javascript" language="Javascript">
var req = createXMLHttpRequest();

// this function is for labeling songs as requests in traffic mode
function setRequest(airId) {
  if (document.getElementById('request[' + airId + ']').checked == true) {
     req.open('get', './includes/ajax_func.php?do=setRequest&id=' + airId);
     req.onreadystatechange = handleResponse;
     req.send(null);
  } else if (document.getElementById('request[' + airId + ']').checked == false) {
     req.open('get', './includes/ajax_func.php?do=unsetRequest&id=' + airId);
     req.onreadystatechange = handleResponse;
     req.send(null);
  }
}

//this function is for deleting CD data from the library, including their tracks; it does not yet trigger file removal
function delAlbum(albID) {
   if (confirm('Are you sure you want to remove this album and all its tracks from the database?  This cannot be undone!')) {
//      alert('../includes/ajax_func.php?do=delAlbum&albId=' + albID);
      req.open('get', '../includes/ajax_func.php?do=delAlbum&albId=' + albID);
      req.onreadystatechange = handleResponse;
      req.send(null);
   }
}

function pickAlb (albId, usrId) {
   req.open('get', './includes/ajax_func.php?do=pickAlb&albId=' + albId + '&usrId=' + usrId);
   req.onreadystatechange = handleResponse;
   req.send(null);
}

function handleResponse() {
if(req.readyState == 4){
    var response = req.responseText;
    var update = new Array();
if(response.indexOf('||' != -1)) {
      update = response.split('||');
      document.getElementById(update[0]).innerHTML = update[1];
    }
  }
}

function openLink (href) {
//   parent.document.getElementById('contextMenu').className = 'hidden';
   parent.frames['main'].location.href = href;
   parent.destroyContext();
}

</script>
<!--//end ajax crap code 2 of 2 -->

<style type="text/css"><?php
include('../themes/'.$theme.'/context.css');
?></style>

</head>
<body>
<input type="hidden" id="albInfoTitle" value="hey hoser" />
<span style="position: absolute; font-family: sans-serif; font-weight:bold; top: 1px; right: 1px;" ><?php
if ($_REQUEST['selectedAlbum']) {
   ?><a href="<?php echo($_SERVER[PHP_SELF].'?artist='.$_REQUEST[artist]) ?>"><img style="border: none;" src="../themes/<?=$theme?>/img/back.png" height="16" width="16" alt="go back" /><small>back</small></a><?php
}
?></span>


<?php
if ($_REQUEST['albId']) $albId = $_REQUEST['albId'];

if (!$albId) {
   $fixedArtist = stripslashes($artist = htmlspecialchars_decode($_REQUEST['artist'], ENT_QUOTES));
   $albCountQuery = mysql_query("SELECT COUNT(`title`) FROM `album` WHERE LOWER(`artist`)='".$_REQUEST['artist']."' ");
   $albInfoQuery = mysql_query("SELECT * FROM `album` WHERE LOWER(`artist`)='".$artist."' ORDER BY `add_date` DESC ");
   //while ($testArray = mysql_fetch_array($albInfoQuery)){
   //   $resultNumber = count($testArray);
   //}
   $albNum = mysql_result($albCountQuery, 0);//count($albInfo);
   if ($albNum > 1) {
//      <h3> echo($artist) </h3>
      ?>
      <a target="_new" href="http://bandtoband.com/index.php?Page=Search&Keywords=<?=htmlspecialchars($fixedArtist)?>"><img style="border: none;" src="../themes/<?=$theme?>/img/b2b.png" height="16" width="16" /></a>
      <a target="_new" href="http://www.last.fm/music/<?=htmlspecialchars(str_replace(' ', '+', $fixedArtist))?>"<img style="border: none;" src="../themes/<?=$theme?>/img/lastfm.gif" height="16" width="16" /></a>
      <a target="_new" href="http://www.musicbrainz.org/newsearch.html?table=artist&search=<?=htmlspecialchars($fixedArtist)?>"><img style="border: none;" src="../themes/<?=$theme?>/img/musicbrainz.png" height="16" width="16" /></a>
      <a target="_new" href="http://www.allmusic.com/cg/amg.dll?p=amg&sql=1:<?=htmlspecialchars(str_replace(' ', '|', $fixedArtist))?>"><img style="border: none;" src="../themes/<?=$theme?>/img/allmusic.png" height="16" width="16" /></a>
      <a target="_new" href="http://en.wikipedia.org/wiki/<?=str_replace(' ', '_', htmlspecialchars($fixedArtist))?>"><img style="border: none;" src="../themes/<?=$theme?>/img/wikipedia.png" height="16" width="16" /></a>
      <a target="_new" href="http://www.pitchforkmedia.com/cgi-bin/search2/search.cgi?terms=<?=htmlspecialchars($fixedArtist)?>"><img style="border: none;" src="../themes/<?=$theme?>/img/pitchfork.png" height="16" width="16" /></a>
      <br />
      <?php echo($albNum) ?> albums from this artist:<br />
      <?php
      while ($albInfo = mysql_fetch_array($albInfoQuery)) {
         //if (($albInfo['add_date']) && ($albInfo['add_date'] < '1970-00-00')) $releaseDate = ' (' . date('Y', $albInfo['add_date']) . ')';
         
         echo ('<a href="'.$_SERVER['PHP_SELF'].'?albId='.$albInfo[cddbid].'&selectedAlbum=true&artist='.$_REQUEST['artist'].'">'.$albInfo['title']); //.'<br />');// . $releaseDate . '<br />');
         $trkCountQuery = mysql_query("SELECT COUNT(`trackno`) FROM `track` WHERE `cddbid`='".$albInfo['cddbid']."' ");
         echo (' (' . mysql_result($trkCountQuery, 0) . ')');
         if ($rights > 2) { 
            ?><a href="" onClick="openLink('../main.php?mode=albEdit&albId=<?=$albInfo[cddbid]?>')"><img style="border: 0px;" height="16" width="16" title="edit this album" alt="edit" src="../themes/<?=$theme?>/img/edit.png" /></a><?php
         }
         echo ('<br />' . "\n");
      }
   } else {
      $albInfo = mysql_fetch_array($albInfoQuery);
      $albId = $albInfo[cddbid];
   }
}

if ($albId) {
   if (!$albInfo) {
      $albInfoQuery = mysql_query("SELECT * FROM `album` WHERE `cddbid`='".$albId."' LIMIT 1");
      $albInfo = mysql_fetch_array($albInfoQuery);
   }


   echo('<b><a onClick="openLink(\'../main.php?mode=albEdit&albId='.$albId.'\')">'.$albInfo[title]);
   ?></a></b><?php 
   if ($rights > 2) { 
      ?>
   <a href="" onClick="delAlbum(<?php echo($albId) ?>)"><img style="border: 0px;" height="16" width="16" title="delete this album" alt="trash, delete" src="../themes/<?=$theme?>/img/trash.png" /></a>
   <a href="" onClick="openLink('../main.php?mode=albEdit&albId=<?=$albInfo[cddbid]?>')"><img style="border: 0px;" height="16" width="16" title="edit this album" alt="edit" src="../themes/<?=$theme?>/img/edit.png" /></a>
      <?php
   }
   if ($albInfo[status] == 2 && $albInfo[slot]) echo (' [slot #'.$albInfo[slot].']');
   ?>
   <?php 
   //echo($albInfo[artist] . '</i>
   echo('<br /><small>' . $albInfo[review] . '</small><br />');
   
   $trkQuery = mysql_query("SELECT * FROM `track` WHERE `cddbid` = '".$albId."' ORDER BY `trackno` ASC ");
   while ($trk = mysql_fetch_array($trkQuery)) {
      if ($trk[virtue] > 2) {
         echo('<a onClick="openLink(\'../main.php?mode=traffic&do=air_add&trkId='.$trk[id].'\')">'.$trk[trackno].'. '.$trk[title].'</a>'.' <a onClick="openLink(\'../'.$PHPMP_HTTP_DIR.'playlist.php?command=add&arg='.ltrim($trk[filepath], $MUSIC_DIR).$trk[filename].'/\')">+&gt;&gt;</a>');
      } else {
         echo($trk[trackno].'. '.$trk[title].'<img src="../themes/'.$theme.'/img/blank.png" height="13" width="1" />');
      }
   if ($trk[virtue] > 3) echo('<img src="../themes/'.$theme.'/img/star_selected.png" />');
   if ($trk[virtue] > 4) echo('<img src="../themes/'.$theme.'/img/star_selected.png" />');
   if ($trk[virtue] > 5) echo('<img src="../themes/'.$theme.'/img/star_selected.png" />');
   if ($trk[virtue] > 2) echo('<img src="../themes/'.$theme.'/img/blank.png" height="13" width="1" />');
   if ($albInfo[is_comp]) echo (' by '.$trk[artist]);
   if ($trk[time]) echo (' <span class="textComment">'.date("i:s", strtotime($trk[time])).'</span>');
   echo('<br />');
   if ($trk[comment]) echo('<span style="margin-left: 20px;" class="textComment">'.$trk[comment].'</span><br />');
   }
}
?>
</body>
</html>
